<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../../static/lib/layui-v2.6.3/css/layui.css" media="all">
	<link rel="stylesheet" href="../../../static/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item">
		<h2 align="center">新增班级</h2>
	</div>
	
	<div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">学校</label>
	    <div class="layui-input-block" >
			<select name="college" id="collegeName" lay-verify="required"  lay-filter="ceshi" lay-reqtext="学校不能为空">
			</select>
	    </div>
	</div>
    <div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">年级</label>
	    <div class="layui-input-block">
			<select name="grade" id="gradeName" lay-verify="required" lay-reqtext="年级不能为空">
            <option value=''></option>

			</select>
	    </div>
	</div>
    <div class="layui-form-item layui-form-text">
	    <label class="layui-form-label required">班级</label>
	    <div class="layui-input-block">
			<select name="class" id="className" lay-verify="required" lay-reqtext="年级不能为空">
            <option value=''></option>
                <option value="一班">一班</option>
                <option value="二班">二班</option>
                <option value="三班">三班</option>
                <option value="四班">四班</option>
                <option value="五班">五班</option>

			</select>
	    </div>
	</div>
    
	


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../../static/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>

<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
		
		// ajax 头部增加csrf_token
		var token = "{{ csrf_token }}"
		$.ajaxSetup({
		    headers: { 'X-CSRFTOKEN': `${token}` }, // 这里是headers，不是data,  CSRF
		});
		
		
		$(function(){   //js 加载时，AJAX请求
			  $.ajax({
				url:'/college',
				type:'get',
				dataType:'json',
				success:function(result){
					var list = result.data;

					var str = "<option value=''></option>"
					for (var i=0; i<list.length;i++) {
						str += "<option value=\""+list[i].id+"\">"+ list[i].name+"</option>"
					}
				    console.log(str);
					$('#collegeName').html(str);
					form.render('select')   //重新渲染 form select内容
				},
				error:function(){
					layer.msg('部门查询API出错！');
				}
			})
		});
		
	
        //监听提交
        form.on('submit(saveBtn)', function (data) {

			//传给后台
			$.ajax({
				url:"/class",
				type:"post",
				data:data.field,

				success:function(result){
					if (result.code===0){
                        layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
						  {#parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页#}
						  parent.layui.table.reload('currentTableId');
						})
                    }
                    else {
                        layer.alert(result.msg);
                    }
					
				},
				error:function(result){
					alert("接口错误！！！");
				}
				});
			
			return false;
			
        });

    });
    //# 当学校的下拉列表改变时，查询年级名称，用ajax来发送id，后台获取数据库中的年级
    layui.use(['layer', 'jquery', 'form'], function() {
				var layer = layui.layer,
					$ = layui.jquery,
					form = layui.form;
				form.on('select(ceshi)', function(data) {
                    var college_name=data.elem.innerText;//在html获取选取的学校ID

                    $.ajax({    //初始化AJAX请求
                    url:'/grade',    //设置请求地址(=localhost:8000/hr/ajax/load-cities/)
                    data:{
                        'college_name': college_name  //countryId传到GET参数
                        },
                    dataType: "JSON",
                    success:function(result){
					var list = result.data;

					var str = "<option value=''></option>"
					for (var i=0; i<list.length;i++) {
						str += "<option value=\""+list[i].id+"\">"+ list[i].name+"</option>"
					}
				    console.log(str);
					$('#gradeName').html(str);
					form.render('select')   //重新渲染 form select内容
				},
           });
				})});
</script>



</body>
</html>